package com.zang.blogz.strategy.context;

import cn.dev33.satoken.stp.SaTokenInfo;
import com.zang.blogz.enmus.LoginTypeEnum;
import com.zang.blogz.model.output.UserInfoVO;
import com.zang.blogz.result.Result;
import io.github.vampireachao.stream.core.optional.Opp;
import com.zang.blogz.strategy.SocialLoginStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

/**
 * @Author: ZVerify
 * @Description:  第三方登录策略上下文
 * @DateTime: 2022/9/2 9:42
 **/
@Service
public class SocialLoginStrategyContext {

    @Autowired
    private Map<String, SocialLoginStrategy> socialLoginStrategyMap;

    /**
     * 执行第三方登录策略
     *
     * @param data          数据
     * @param loginTypeEnum 登录枚举类型
     * @return {@link UserInfoVO} 用户信息
     */
    public Result<UserInfoVO> executeLoginStrategy(String data, LoginTypeEnum loginTypeEnum) {
        Opp<Result<UserInfoVO>> opp = Opp.ofTry(() -> socialLoginStrategyMap.get(loginTypeEnum.getStrategy()).login(data, loginTypeEnum));

        return opp.get();

    }
}
